using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX.DirectDraw;
using TowerDefense_Dx.Core;

namespace TowerDefense_Dx.GameLogic
{
    public class Wave
    {
        private bool _isEnemySpawning = false;
        private int _enemyCount = 0;
        private double _spawnTime;
        private double _elapsedTime;
        private PointF _beginPosition;
        private DxInitGraphics _graphic;
        private string _enemySpritePath;
        private int _waveNumber;
        public int WaveNumber
        {
            get { return _waveNumber; }
        }
        private int _numOfEnemy;
        public int NumOfEnemy
        {
            get { return _numOfEnemy; }
            set { _numOfEnemy = value; }
        }
        private List<Enemy> _enemyList = new List<Enemy>();

        public List<Enemy> EnemyList
        {
            get { return _enemyList; }
            set { _enemyList = value; }
        }
        private List<DxAnimation> _animationList = new List<DxAnimation>();
        private bool _roundOver;

        public bool RoundOver
        {
            get { return _roundOver; }           
        }

        private int[] _wayPoint;
        public int[] WayPoint
        {
            get { return _wayPoint; }
            set { _wayPoint = value; }
        }      



        public Wave(DxInitGraphics graphic,int waveNumber, string enemySpritePath, int numOfEnemy, float spawnTime, int[] wayPoint)
        {
            _graphic = graphic;
            _waveNumber = waveNumber;
            _enemySpritePath = enemySpritePath;
            _numOfEnemy = numOfEnemy;
            _spawnTime = spawnTime;
            _wayPoint = wayPoint;

            _beginPosition = new PointF(((_wayPoint[0] - 1) % 16) * 32 +3, ((_wayPoint[0] - 1) / 16) * 32 + 96);
        }
        public void Start()
        {
            _isEnemySpawning = true;
            _roundOver = false;
        }
        public void SpawnEnemy(double deltaTime)
        {
            _elapsedTime += deltaTime;
            if (_elapsedTime >= _spawnTime)
            {
                _elapsedTime = 0;
                Enemy enemy = new Enemy(_enemySpritePath, _graphic.GraphicsDevice, 32, 32, _beginPosition);
                _enemyList.Add(enemy);
                _enemyCount++;

                DxAnimation animation = new DxAnimation(enemy, 250,1,3, true);
                _animationList.Add(animation);                    
            }
        }

        public void EnemyUpdate(double deltaTime,Player player)
        {
            if (_enemyCount == _numOfEnemy)
            {
                _isEnemySpawning = false;
                _roundOver = true;
            }
            if (_isEnemySpawning)
            {
                SpawnEnemy(deltaTime);
            }
            if (_enemyList != null)
            {
                for (int i = 0; i < _enemyList.Count; i++)
                {
                    _enemyList[i].Move(_wayPoint);
                    _animationList[i].Update(deltaTime, _enemyList[i].CurrentDirection);
                    if (_enemyList[i].EnemyState != TowerDefense_Dx.GameLogic.Enemy.ESTATE.NORMAL)
                    {                       
                        if (_enemyList[i].EnemyState == Enemy.ESTATE.DEAD)
                        {
                            player.GetBounty(_enemyList[i]);
                        }
                        else 
                        {
                            player.Lives--;
                        }
                        _enemyList.Remove(_enemyList[i]);
                        _animationList.Remove(_animationList[i]);
                        i--;
                    }
                }
            }
        }
        public void Draw(Surface destSurface)
        {
            for (int i = 0; i < _animationList.Count; i++)
            {
                _animationList[i].Draw((int)_enemyList[i].Position.X,(int) _enemyList[i].Position.Y, destSurface);
            }
        }
    }
}
